<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="baidu-site-verification" content="zD47G9xe42" />


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="MySQL巩固与加强," />





  <link rel="alternate" href="/atom.xml" title="uiste" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="数据类型数据类型——值类型整型


整型
占用字节
范围
范围




tinyint
1
-2^7~2^7-1
-128~127


smallint
2
-2^15~2^15-1
-32768~32767


mediumint
3
-2^23~2^23-1
-8388608~8388607


int
4
-2^31~2^31-1
-2147483648~2147483647


bigin">
<meta property="og:type" content="article">
<meta property="og:title" content="Day8-MySQL 数据类型、列属性、数据完整性">
<meta property="og:url" content="http://blog.uiste.com/20160830-1.html">
<meta property="og:site_name" content="uiste">
<meta property="og:description" content="数据类型数据类型——值类型整型


整型
占用字节
范围
范围




tinyint
1
-2^7~2^7-1
-128~127


smallint
2
-2^15~2^15-1
-32768~32767


mediumint
3
-2^23~2^23-1
-8388608~8388607


int
4
-2^31~2^31-1
-2147483648~2147483647


bigin">
<meta property="og:updated_time" content="2016-09-01T14:26:24.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Day8-MySQL 数据类型、列属性、数据完整性">
<meta name="twitter:description" content="数据类型数据类型——值类型整型


整型
占用字节
范围
范围




tinyint
1
-2^7~2^7-1
-128~127


smallint
2
-2^15~2^15-1
-32768~32767


mediumint
3
-2^23~2^23-1
-8388608~8388607


int
4
-2^31~2^31-1
-2147483648~2147483647


bigin">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://blog.uiste.com/20160830-1.html"/>

  <title>
 Day8-MySQL 数据类型、列属性、数据完整性 | uiste 
</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?b58b87af012832b8ae35e33bb45b44fa";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">uiste</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">uiste个人博客</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      

            <li class="menu-item menu-item-search"> <a title="把这个链接拖到你的Chrome收藏夹工具栏中" href='javascript:(function() {
    function c() {
        var e = document.createElement("link");
        e.setAttribute("type", "text/css");
        e.setAttribute("rel", "stylesheet");
        e.setAttribute("href", f);
        e.setAttribute("class", l);
        document.body.appendChild(e)
    }
 
    function h() {
        var e = document.getElementsByClassName(l);
        for (var t = 0; t < e.length; t++) {
            document.body.removeChild(e[t])
        }
    }
 
    function p() {
        var e = document.createElement("div");
        e.setAttribute("class", a);
        document.body.appendChild(e);
        setTimeout(function() {
            document.body.removeChild(e)
        }, 100)
    }
 
    function d(e) {
        return {
            height : e.offsetHeight,
            width : e.offsetWidth
        }
    }
 
    function v(i) {
        var s = d(i);
        return s.height > e && s.height < n && s.width > t && s.width < r
    }
 
    function m(e) {
        var t = e;
        var n = 0;
        while (!!t) {
            n += t.offsetTop;
            t = t.offsetParent
        }
        return n
    }
 
    function g() {
        var e = document.documentElement;
        if (!!window.innerWidth) {
            return window.innerHeight
        } else if (e && !isNaN(e.clientHeight)) {
            return e.clientHeight
        }
        return 0
    }
 
    function y() {
        if (window.pageYOffset) {
            return window.pageYOffset
        }
        return Math.max(document.documentElement.scrollTop, document.body.scrollTop)
    }
 
    function E(e) {
        var t = m(e);
        return t >= w && t <= b + w
    }
 
    function S() {
        var e = document.createElement("audio");
        e.setAttribute("class", l);
        e.src = i;
        e.loop = false;
        e.addEventListener("canplay", function() {
            setTimeout(function() {
                x(k)
            }, 500);
            setTimeout(function() {
                N();
                p();
                for (var e = 0; e < O.length; e++) {
                    T(O[e])
                }
            }, 15500)
        }, true);
        e.addEventListener("ended", function() {
            N();
            h()
        }, true);
        e.innerHTML = " <p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p> <p>";
        document.body.appendChild(e);
        e.play()
    }
 
    function x(e) {
        e.className += " " + s + " " + o
    }
 
    function T(e) {
        e.className += " " + s + " " + u[Math.floor(Math.random() * u.length)]
    }
 
    function N() {
        var e = document.getElementsByClassName(s);
        var t = new RegExp("\\b" + s + "\\b");
        for (var n = 0; n < e.length; ) {
            e[n].className = e[n].className.replace(t, "")
        }
    }
 
    var e = 30;
    var t = 30;
    var n = 350;
    var r = 350;
    var i = "//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3";
    var s = "mw-harlem_shake_me";
    var o = "im_first";
    var u = ["im_drunk", "im_baked", "im_trippin", "im_blown"];
    var a = "mw-strobe_light";
    var f = "//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake-style.css";
    var l = "mw_added_css";
    var b = g();
    var w = y();
    var C = document.getElementsByTagName("*");
    var k = null;
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            if (E(A)) {
                k = A;
                break
            }
        }
    }
    if (A === null) {
        console.warn("Could not find a node of the right size. Please try a different page.");
        return
    }
    c();
    S();
    var O = [];
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            O.push(A)
        }
    }
})()    '>High</a> </li>

    </ul>
  

 
    <div class="site-search">
      
  <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>


    </div>
   
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Day8-MySQL 数据类型、列属性、数据完整性
              
            
          </h1>
        

        <div class="post-meta">
        
  <span id="busuanzi_container_page_pv">阅读量 <span id="busuanzi_value_page_pv"></span> 次</span>

          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-08-30T17:18:51+08:00" content="2016-08-30">
              2016-08-30
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/MySQL/" itemprop="url" rel="index">
                    <span itemprop="name">MySQL</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/20160830-1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="20160830-1.html" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h3><h4 id="数据类型——值类型"><a href="#数据类型——值类型" class="headerlink" title="数据类型——值类型"></a>数据类型——值类型</h4><h5 id="整型"><a href="#整型" class="headerlink" title="整型"></a>整型</h5><table>
<thead>
<tr>
<th>整型</th>
<th style="text-align:center">占用字节</th>
<th style="text-align:center">范围</th>
<th style="text-align:center">范围</th>
</tr>
</thead>
<tbody>
<tr>
<td>tinyint</td>
<td style="text-align:center">1</td>
<td style="text-align:center">-2^7~2^7-1</td>
<td style="text-align:center">-128~127</td>
</tr>
<tr>
<td>smallint</td>
<td style="text-align:center">2</td>
<td style="text-align:center">-2^15~2^15-1</td>
<td style="text-align:center">-32768~32767</td>
</tr>
<tr>
<td>mediumint</td>
<td style="text-align:center">3</td>
<td style="text-align:center">-2^23~2^23-1</td>
<td style="text-align:center">-8388608~8388607</td>
</tr>
<tr>
<td>int</td>
<td style="text-align:center">4</td>
<td style="text-align:center">-2^31~2^31-1</td>
<td style="text-align:center">-2147483648~2147483647</td>
</tr>
<tr>
<td>bigint</td>
<td style="text-align:center">8</td>
<td style="text-align:center">-2^63~2^63-1</td>
<td style="text-align:center">-</td>
</tr>
</tbody>
</table>
<blockquote>
<p>一个字节=8位，8位就是8个二进制数</p>
</blockquote>
<ul>
<li>整型支持显示宽度：最小的显示位数，比如int(5)，如果数值的位数小于5位，前面加上前导0。比如输入12，显示00012；大于5位就不添加前导0</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">mysql&gt; describe student;</div><div class="line">+-------+--------------------------+------+-----+---------+----------------+</div><div class="line">| Field | Type                     | Null | Key | Default | Extra          |</div><div class="line">+-------+--------------------------+------+-----+---------+----------------+</div><div class="line">| id    | int(11)                  | NO   | PRI | NULL    | auto_increment |</div><div class="line">| name  | char(1)                  | YES  |     | NULL    |                |</div><div class="line">| num   | int(8) unsigned zerofill | YES  |     | NULL    |                |</div><div class="line">+-------+--------------------------+------+-----+---------+----------------+</div><div class="line">3 rows in set (0.01 sec)</div><div class="line"></div><div class="line">mysql&gt; select * from student;</div><div class="line">+----+------+------------+</div><div class="line">| id | name | num        |</div><div class="line">+----+------+------------+</div><div class="line">|  1 | a    |   00002485 |</div><div class="line">|  2 | b    |   03678928 |</div><div class="line">|  3 | c    | 1234567890 |</div><div class="line">+----+------+------------+</div><div class="line">3 rows in set (0.00 sec)</div></pre></td></tr></table></figure>
<blockquote>
<p>脚下留心：必须结合zerofill才起作用<br>显示宽度不决定显示范围，只是在数值不够指定位数时用0做前导</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">mysql&gt; alter table student add num2 tinyint;</div><div class="line">Query OK, 0 rows affected (0.05 sec)</div><div class="line">Records: 0  Duplicates: 0  Warnings: 0</div><div class="line"></div><div class="line">mysql&gt; insert into student values (null,&apos;d&apos;,&apos;1&apos;,128);</div><div class="line">Query OK, 1 row affected, 1 warning (0.01 sec)</div><div class="line"></div><div class="line">mysql&gt; select * from student;</div><div class="line">+----+------+------------+------+</div><div class="line">| id | name | num        | num2 |</div><div class="line">+----+------+------------+------+</div><div class="line">|  1 | a    |   00002485 | NULL |</div><div class="line">|  2 | b    |   03678928 | NULL |</div><div class="line">|  3 | c    | 1234567890 | NULL |</div><div class="line">|  4 | d    |   00000001 |  127 |</div><div class="line">+----+------+------------+------+</div><div class="line">4 rows in set (0.00 sec)</div></pre></td></tr></table></figure>
<blockquote>
<p>超出最大范围会有警告提示，同时值保存最大范围值</p>
</blockquote>
<ul>
<li>无符号整数（unsigned）：无符号数没有负数，正数部分是有符号的两倍</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into student values (null,666);</div><div class="line">Query OK, 1 row affected, 1 warning (0.01 sec)</div><div class="line"></div><div class="line">mysql&gt; select * from student;</div><div class="line">+----+------+</div><div class="line">| id | num  |</div><div class="line">+----+------+</div><div class="line">|  7 |  255 |</div><div class="line">+----+------+</div><div class="line">1 row in set (0.00 sec)</div></pre></td></tr></table></figure>
<h5 id="浮点型（保存近似值小数）"><a href="#浮点型（保存近似值小数）" class="headerlink" title="浮点型（保存近似值小数）"></a>浮点型（保存近似值小数）</h5><table>
<thead>
<tr>
<th>浮点型</th>
<th style="text-align:center">占用字节</th>
<th style="text-align:center">范围</th>
</tr>
</thead>
<tbody>
<tr>
<td>float（单精度）</td>
<td style="text-align:center">4</td>
<td style="text-align:center">-3.4E+38~3.4E+38</td>
</tr>
<tr>
<td>double（双精度）</td>
<td style="text-align:center">8</td>
<td style="text-align:center">-1.8E+308~1.8E+308</td>
</tr>
</tbody>
</table>
<ul>
<li>浮点数声明:  float(M,D)   double(M,D)<br>M：总位数<br>D：小数位数</li>
</ul>
<blockquote>
<p>浮点数也是支持显示宽度和无符号数</p>
</blockquote>
<ul>
<li>浮点数支持科学计数法<br><code>insert into uiste values (5E2,4E-1);</code></li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into student values (null,5E2,4E-1);</div><div class="line">Query OK, 1 row affected (0.00 sec)</div><div class="line"></div><div class="line">mysql&gt; select * from student;</div><div class="line">+----+--------------+------+</div><div class="line">| id | num1         | num2 |</div><div class="line">+----+--------------+------+</div><div class="line">|  7 |         NULL | NULL |</div><div class="line">|  8 | 000000000500 | 00.4 |</div><div class="line">+----+--------------+------+</div><div class="line">2 rows in set (0.00 sec)</div></pre></td></tr></table></figure>
<ul>
<li>浮点的精度可能会丢失【指的是小数】<br><code>insert into uiste values (99.999999999);</code></li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into student values (null,9.99999999999999,1.1111111111111);</div><div class="line">Query OK, 1 row affected (0.01 sec)</div><div class="line"></div><div class="line">mysql&gt; select * from student;</div><div class="line">+----+--------------+------+</div><div class="line">| id | num1         | num2 |</div><div class="line">+----+--------------+------+</div><div class="line">|  7 |         NULL | NULL |</div><div class="line">|  8 | 000000000500 | 00.4 |</div><div class="line">|  9 | 000000000010 | 01.1 |</div><div class="line">+----+--------------+------+</div><div class="line">3 rows in set (0.00 sec)</div></pre></td></tr></table></figure>
<blockquote>
<p>小数部分如果超过D的限制会四舍五入，整数部分不能超过M-D的限制</p>
</blockquote>
<ul>
<li>定点数<br>语法：decimal(M,D)</li>
</ul>
<blockquote>
<p>定点数是变长的，大致每9个数字用4个字节来存储。定点数之所以能保存精确的小数，因为整数和小数是分开存储的。占用的资源比浮点数要多。<br>定点数和浮点数都支持显示宽度和无符号数</p>
</blockquote>
<h4 id="数据类型——字符型"><a href="#数据类型——字符型" class="headerlink" title="数据类型——字符型"></a>数据类型——字符型</h4><table>
<thead>
<tr>
<th>数据类型</th>
<th style="text-align:center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>char(L)</td>
<td style="text-align:center">定长</td>
</tr>
<tr>
<td>varchar(L)</td>
<td style="text-align:center">变长</td>
</tr>
<tr>
<td>tinytext</td>
<td style="text-align:center">2^8-1=255</td>
</tr>
<tr>
<td>text</td>
<td style="text-align:center">2^16-1=65535</td>
</tr>
<tr>
<td>mediumtext</td>
<td style="text-align:center">2^24-1</td>
</tr>
<tr>
<td>longtext</td>
<td style="text-align:center">2^32-1</td>
</tr>
</tbody>
</table>
<blockquote>
<p>char(10)和varchar(10)的区别？<br>答：相同点：它们最多只能保存10个字符；<br>不同点：char不回收富余的字符，varchar会回收富余的字符。<br>char效率高，浪费空间，varchar节省空间，效率比char低。</p>
<p>char的最大长度是255。varchar理论长度是65535字节,实际根本达不到。具体长度与字符编码有关</p>
<p>大块文本（text）不计算在总长度中,一个大块文本只占用10个字节来保存文本的地址。</p>
</blockquote>
<h4 id="数据类型——枚举（enum）"><a href="#数据类型——枚举（enum）" class="headerlink" title="数据类型——枚举（enum）"></a>数据类型——枚举（enum）</h4><p>从集合中选择一个数据（单选）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create database uiste charset=utf8;</div><div class="line">Query OK, 1 row affected (0.01 sec)</div><div class="line"></div><div class="line">mysql&gt; create table student(</div><div class="line">    -&gt; name varchar(20),</div><div class="line">    -&gt; sex enum(&apos;男&apos;,&apos;女&apos;,&apos;保密&apos;)</div><div class="line">    -&gt; )charset=utf8;</div><div class="line">Query OK, 0 rows affected (0.02 sec)</div><div class="line"></div><div class="line">mysql&gt; insert into student values (&apos;杜敏&apos;,&apos;男&apos;);</div><div class="line">Query OK, 1 row affected (0.00 sec)</div><div class="line"></div><div class="line">mysql&gt; insert into student values (&apos;钟程程&apos;,&apos;男或女&apos;);</div><div class="line">Query OK, 1 row affected, 1 warning (0.01 sec)</div><div class="line"></div><div class="line">mysql&gt; select * from student;</div><div class="line">+-----------+------+</div><div class="line">| name      | sex  |</div><div class="line">+-----------+------+</div><div class="line">| 杜敏      | 男   |</div><div class="line">| 钟程程    |      |</div><div class="line">+-----------+------+</div><div class="line">2 rows in set (0.00 sec)</div></pre></td></tr></table></figure>
<blockquote>
<p>使用枚举进行数据选择的时候，没有屏蔽错误信息只是提示了警告信息</p>
<p>MySQL的枚举类型是通过整数来管理的，第一个值是1，第二个值是2，以此类推<br>既然枚举在数据库内部存储的是整数，那么可以直接插入数字</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">mysql&gt; select sex+0 from student;</div><div class="line">+-------+</div><div class="line">| sex+0 |</div><div class="line">+-------+</div><div class="line">|     1 |</div><div class="line">|     0 |</div><div class="line">+-------+</div><div class="line">2 rows in set (0.01 sec)</div><div class="line"></div><div class="line">mysql&gt; insert into student values (&apos;赵哥&apos;,2);</div><div class="line">Query OK, 1 row affected (0.01 sec)</div><div class="line"></div><div class="line">mysql&gt; select * from student;</div><div class="line">+-----------+------+</div><div class="line">| name      | sex  |</div><div class="line">+-----------+------+</div><div class="line">| 杜敏      | 男   |</div><div class="line">| 钟程程    |      |</div><div class="line">| 赵哥      | 女   |</div><div class="line">+-----------+------+</div><div class="line">3 rows in set (0.00 sec)</div></pre></td></tr></table></figure>
<p><strong>枚举的优点：</strong><br>1、    运行速度快（数字比字符串运算速度快）<br>2、    限制数据<br>3、    节省空间</p>
<blockquote>
<p>已知枚举占用2个字节，请问最多有多少个枚举值？<br>答：2个字节=16位，可以保存216个数字（0-65535），枚举是从1开始，所以枚举最多可以有65535个枚举值</p>
</blockquote>
<h4 id="数据类型——集合（set"><a href="#数据类型——集合（set" class="headerlink" title="数据类型——集合（set)"></a>数据类型——集合（set)</h4><p>从集合中选择一些数据（多选）</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table student(</div><div class="line">    -&gt; name char,</div><div class="line">    -&gt; hobby set(&apos;看书&apos;,&apos;玩游戏&apos;,&apos;敲代码&apos;,&apos;看电视&apos;)</div><div class="line">    -&gt; )charset=utf8;</div><div class="line">Query OK, 0 rows affected (0.03 sec)</div></pre></td></tr></table></figure>
<p>插入集合的值</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into student values (&apos;杜敏&apos;,&apos;玩游戏&apos;);</div><div class="line">Query OK, 1 row affected, 1 warning (0.01 sec)</div><div class="line">mysql&gt; insert into student values (&apos;小兰&apos;,&apos;看书,敲代码&apos;);</div><div class="line">Query OK, 1 row affected, 1 warning (0.01 sec)</div><div class="line">mysql&gt; insert into student values (&apos;兰&apos;,&apos;敲代码,看书&apos;);</div><div class="line">Query OK, 1 row affected (0.01 sec)</div><div class="line"></div><div class="line">mysql&gt; select * from student;</div><div class="line">+------+------------------+</div><div class="line">| name | hobby            |</div><div class="line">+------+------------------+</div><div class="line">| 杜   | 玩游戏           |</div><div class="line">| 小   | 看书,敲代码      |</div><div class="line">| 兰   | 看书,敲代码      |</div><div class="line">+------+------------------+</div><div class="line">3 rows in set (0.00 sec)</div></pre></td></tr></table></figure>
<blockquote>
<p>插入的顺序不一样，但是显示的顺序是一样的</p>
<p>集合和枚举一样，每个集合的元素都分配一个固定的值，分配的方式从左往右按2的0、1、2、…次方</p>
</blockquote>
<p>如果是多选，他们的关系是按位或<br>按位与：相同的位上都是1，结果才为1，只要有一个为0，结果为0。<br>按位或：相同的位上只要有一个为1结果就是1。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">mysql&gt; select hobby+0 from student;</div><div class="line">+---------+</div><div class="line">| hobby+0 |</div><div class="line">+---------+</div><div class="line">|       2 |</div><div class="line">|       5 |</div><div class="line">|       5 |</div><div class="line">+---------+</div><div class="line">3 rows in set (0.01 sec)</div></pre></td></tr></table></figure>
<blockquote>
<p>思考：已知集合占用8个字节，最多可以表示几个选项？<br>答：8个字节=64位，一个位表示1个选项，最多可以表示64个选项。</p>
</blockquote>
<h4 id="数据类型——日期类型"><a href="#数据类型——日期类型" class="headerlink" title="数据类型——日期类型"></a>数据类型——日期类型</h4><table>
<thead>
<tr>
<th>数据类型</th>
<th style="text-align:center">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>datetime</td>
<td style="text-align:center">日期时间，占用8个字节</td>
</tr>
<tr>
<td>date</td>
<td style="text-align:center">日期</td>
</tr>
<tr>
<td>time</td>
<td style="text-align:center">时间</td>
</tr>
<tr>
<td>timestamp</td>
<td style="text-align:center">时间戳   占用4个字节</td>
</tr>
<tr>
<td>year</td>
<td style="text-align:center">年份   占用1个字节</td>
</tr>
</tbody>
</table>
<ul>
<li>datetime    格式：年-月-日 小时:分钟:秒</li>
</ul>
<p>测试</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into test10 values (&apos;2016-6-24 15:15:15&apos;);	--正确</div><div class="line">mysql&gt; insert into test10 values (&apos;100-6-24 15:15:15&apos;);		--正确</div><div class="line">mysql&gt; insert into test10 values (&apos;9999-6-24 15:15:15&apos;);	--正确</div><div class="line">mysql&gt; insert into test10 values (&apos;10000-6-24 15:15:15&apos;);	--错误</div></pre></td></tr></table></figure>
<blockquote>
<p>datetime保存范围是：1~9999年</p>
</blockquote>
<ul>
<li>timestamp：时间戳</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into test11 values (&apos;2016-6-24 15:15:15&apos;);	--正确的</div><div class="line">mysql&gt; insert into test11 values (&apos;1969-12-31 23:59:59&apos;);	--超过范围</div><div class="line">mysql&gt; insert into test11 values (&apos;2038-01-19 11:14:08&apos;);	--超出范围</div><div class="line">mysql&gt; insert into test11 values (&apos;2038-01-19 11:14:07&apos;);	--正确</div></pre></td></tr></table></figure>
<blockquote>
<p>datetime类型timestamp类型在表现上是一样的。他们的区别：<br>datetime是从1到9999，而timestamp从1970年~2038年，2038年01月19日11:14:07秒以后就超出timestamp范围了。</p>
</blockquote>
<ul>
<li><p>year<br>因为只占用1个字节，最多只能表示255个年份，范围是1901-2155之间的年份</p>
</li>
<li><p>time<br>表示时间或时间间隔，范围是-838:59:59~838:59:59</p>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into test13 values (12:12:12);	--正确</div><div class="line">mysql&gt; insert into test13 values (&apos;212:12:12&apos;);	--正确</div><div class="line">mysql&gt; insert into test13 values (&apos;-838:59:59&apos;);--正确</div><div class="line">mysql&gt; insert into test13 values (&apos;838:59:59&apos;);	--正确</div><div class="line">mysql&gt; insert into test13 values (&apos;839:00:00&apos;);	--错误</div></pre></td></tr></table></figure>
<blockquote>
<p>time支持以天的方式插入</p>
</blockquote>
<p><code>insert into student values (&#39;10 12:12:12&#39;);</code></p>
<h4 id="数据类型——boolean"><a href="#数据类型——boolean" class="headerlink" title="数据类型——boolean"></a>数据类型——boolean</h4><p>MySQL不支持boolean类型，true和false在数据库中对应1和0。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table test1(</div><div class="line">    -&gt; `field` boolean</div><div class="line">    -&gt; )charset=utf8;</div><div class="line">Query OK, 0 rows affected (0.03 sec)</div><div class="line"></div><div class="line">mysql&gt; describe test1;</div><div class="line">+-------+------------+------+-----+---------+-------+</div><div class="line">| Field | Type       | Null | Key | Default | Extra |</div><div class="line">+-------+------------+------+-----+---------+-------+</div><div class="line">| field | tinyint(1) | YES  |     | NULL    |       |</div><div class="line">+-------+------------+------+-----+---------+-------+</div><div class="line">1 row in set (0.01 sec)</div></pre></td></tr></table></figure>
<h4 id="创建数据表的数据类型"><a href="#创建数据表的数据类型" class="headerlink" title="创建数据表的数据类型"></a>创建数据表的数据类型</h4><blockquote>
<p>手机号码一般使用什么数据类型存储?            char<br>性别一般使用什么数据类型存储?                char    enum<br>年龄信息一般使用什么数据类型存储?            tinyint<br>照片信息一般使用什么数据类型存储?            binary(二进制)<br>薪水一般使用什么数据类型存储?                decimal</p>
</blockquote>
<p><strong>一个字段到底选数字还是字符，取决于有没有计算的可能，如果没有计算的可能即使是数字也要用字符类型，比如手机号、QQ号，…</strong></p>
<h3 id="列属性-是否为空-null-not-null"><a href="#列属性-是否为空-null-not-null" class="headerlink" title="列属性-是否为空(null | not null)"></a>列属性-是否为空(null | not null)</h3><p>null：可以为空<br>not null：不可以为空</p>
<h4 id="列属性——默认值（default）"><a href="#列属性——默认值（default）" class="headerlink" title="列属性——默认值（default）"></a>列属性——默认值（default）</h4><blockquote>
<p>如果一个字段没有插入值，可以默认插入一个指定的值<br>没有插入的字段插入默认值<br>default关键字用来插入默认值</p>
</blockquote>
<h4 id="列属性——自动增长（auto-increment）"><a href="#列属性——自动增长（auto-increment）" class="headerlink" title="列属性——自动增长（auto_increment）"></a>列属性——自动增长（auto_increment）</h4><p>字段的值从1开始，每次递增1，特点就在字段中的数据不可能重复，适合为记录生成唯一的id</p>
<blockquote>
<p>在MySQL中，auto_increment必须是主键。但是主键不一定是自动增长的</p>
</blockquote>
<p><strong>既然从1开始，每次递增1，所以一般自动增长都是无符号整数</strong></p>
<blockquote>
<p>如果要给自动增长列插入数据，使用null关键字。<br>自动增长列上的数据被删除，默认情况下此记录的编号不再使用。</p>
<p>思考题：在一个自动增长列上，插入三行，删除两行，插入三行，删除两行，插入三行，删除两行，再插入一个记录编号是多少？<br>答：编号从10开始。删除记录与自动增长的编号没有关系</p>
</blockquote>
<h4 id="列属性——主键（primary-key）"><a href="#列属性——主键（primary-key）" class="headerlink" title="列属性——主键（primary key）"></a>列属性——主键（primary key）</h4><p>主键：唯一标识表中记录的一个或一组列<br>主键的特点：不能重复，不能为空<br>一个表只能有一个主键，主键可以有多个字段组成</p>
<p>主键的作用：</p>
<ul>
<li>保证数据完整性</li>
<li>加快查询速度</li>
</ul>
<h5 id="添加主键：方法一【创建表的时候添加主键】"><a href="#添加主键：方法一【创建表的时候添加主键】" class="headerlink" title="添加主键：方法一【创建表的时候添加主键】"></a>添加主键：方法一【创建表的时候添加主键】</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table stu(</div><div class="line">    -&gt; id int primary key,</div><div class="line">    -&gt; name varchar(20)</div><div class="line">    -&gt; );</div><div class="line">Query OK, 0 rows affected (0.00 sec)</div></pre></td></tr></table></figure>
<h5 id="添加主键：方法二【创建表的时候添加主键】"><a href="#添加主键：方法二【创建表的时候添加主键】" class="headerlink" title="添加主键：方法二【创建表的时候添加主键】"></a>添加主键：方法二【创建表的时候添加主键】</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table stu(</div><div class="line">    -&gt; id int,</div><div class="line">    -&gt; name varchar(20),</div><div class="line">    -&gt; primary key(id)</div><div class="line">    -&gt; );</div></pre></td></tr></table></figure>
<h5 id="添加主键：方法三【更改表的时候添加主键】"><a href="#添加主键：方法三【更改表的时候添加主键】" class="headerlink" title="添加主键：方法三【更改表的时候添加主键】"></a>添加主键：方法三【更改表的时候添加主键】</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table stu(</div><div class="line">    -&gt; id int,</div><div class="line">    -&gt; name varchar(20)</div><div class="line">    -&gt; );</div><div class="line">Query OK, 0 rows affected (0.00 sec)</div><div class="line"></div><div class="line">mysql&gt; alter table stu add primary key (id)</div><div class="line">Query OK, 0 rows affected (0.01 sec)</div><div class="line">Records: 0  Duplicates: 0  Warnings: 0</div></pre></td></tr></table></figure>
<h5 id="查看主键"><a href="#查看主键" class="headerlink" title="查看主键"></a>查看主键</h5><p><code>mysql&gt;desc stu;</code></p>
<h5 id="创建组合键"><a href="#创建组合键" class="headerlink" title="创建组合键"></a>创建组合键</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table stu(</div><div class="line">    -&gt; id int,</div><div class="line">    -&gt; name varchar(20),</div><div class="line">    -&gt; primary key(id,name)</div><div class="line">    -&gt; );</div><div class="line">Query OK, 0 rows affected (0.00 sec)</div></pre></td></tr></table></figure>
<blockquote>
<p>也可以通过更改表的方式来创建组合键<br>alter table stu add primary key(id,name)</p>
</blockquote>
<h5 id="删除主键"><a href="#删除主键" class="headerlink" title="删除主键"></a>删除主键</h5><p><code>mysql&gt;alter table stu drop primary key;</code></p>
<h5 id="选择主键的原则"><a href="#选择主键的原则" class="headerlink" title="选择主键的原则"></a>选择主键的原则</h5><p>1、    最少性：尽量选择一个字段做主键<br>2、    稳定性：尽量选择更新少的列做主键<br>3、    尽量选择数字型的列做主键</p>
<h5 id="主键思考题"><a href="#主键思考题" class="headerlink" title="主键思考题"></a>主键思考题</h5><blockquote>
<p>在主键列输入的数值，允许为空吗?                不能为空<br>一个表可以有多个主键吗?                            不能<br>在一个学校数据库中，如果一个学校内允许重名的学员，但是一个班级内不允许学员重名，可以组合班级和姓名两个字段一起来作为主键吗？可以<br>标识列（自动增长列）允许为字符数据类型吗？        不允许<br>表中没有合适的列作为主键怎么办？                添加一个自动增长列<br>   如果标识列A的初始值为1，增长量为1，则输入三行数据以后，再删除两行，下次再输入数据行的时候，标识值从多少开始？  从4开始</p>
</blockquote>
<h4 id="列属性——唯一键"><a href="#列属性——唯一键" class="headerlink" title="列属性——唯一键"></a>列属性——唯一键</h4><p>特点：不能重复，可以为空<br>一个表可以有多个唯一键<br>作用：<br>1、    保证数据完整性<br>2、    加快数据访问</p>
<h5 id="添加唯一键【创建表的时候添加唯一键】"><a href="#添加唯一键【创建表的时候添加唯一键】" class="headerlink" title="添加唯一键【创建表的时候添加唯一键】"></a>添加唯一键【创建表的时候添加唯一键】</h5><p>unique key<br>unique </p>
<h5 id="添加唯一键【创建表的时候添加唯一键】-1"><a href="#添加唯一键【创建表的时候添加唯一键】-1" class="headerlink" title="添加唯一键【创建表的时候添加唯一键】"></a>添加唯一键【创建表的时候添加唯一键】</h5><p>unique key(stuname)<br>unique(stuadd)</p>
<h5 id="设置名字的唯一键"><a href="#设置名字的唯一键" class="headerlink" title="设置名字的唯一键"></a>设置名字的唯一键</h5><p><code>unique key `name` (`stuname`)</code><br>name：唯一键名字<br>stuname：字段名字</p>
<h5 id="添加唯一键【修改表的时候添加唯一键】"><a href="#添加唯一键【修改表的时候添加唯一键】" class="headerlink" title="添加唯一键【修改表的时候添加唯一键】"></a>添加唯一键【修改表的时候添加唯一键】</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table stu(</div><div class="line">    -&gt; id int primary key,</div><div class="line">    -&gt; stuname varchar(20),</div><div class="line">    -&gt; stuadd varchar(100)</div><div class="line">    -&gt; );</div><div class="line">Query OK, 0 rows affected (0.00 sec)</div></pre></td></tr></table></figure>
<p>添加唯一键</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; alter table stu add unique key(stuname);   --key可以省略</div><div class="line">mysql&gt; alter table stu add unique (stuadd);</div></pre></td></tr></table></figure>
<p>一次添加多个唯一键<br><code>mysql&gt;alter table stu add unique(stuname),add unique(stuadd);</code></p>
<h5 id="查看唯一键"><a href="#查看唯一键" class="headerlink" title="查看唯一键"></a>查看唯一键</h5><p><code>mysql&gt;show create talbe stu\G</code></p>
<h5 id="添加组合唯一键"><a href="#添加组合唯一键" class="headerlink" title="添加组合唯一键"></a>添加组合唯一键</h5><h5 id="删除唯一键"><a href="#删除唯一键" class="headerlink" title="删除唯一键"></a>删除唯一键</h5><p>通过唯一键的名字来删除唯一键<br>语法：<code>alter table 表名 drop index 唯一键名称</code><br><code>mysql&gt;alter talbe stu drop index stuname;</code></p>
<h5 id="列属性——备注（comment）"><a href="#列属性——备注（comment）" class="headerlink" title="列属性——备注（comment）"></a>列属性——备注（comment）</h5><h5 id="SQL注释"><a href="#SQL注释" class="headerlink" title="SQL注释"></a>SQL注释</h5><p>单行注释：–或#<br>多行注释：/<em>    </em>/</p>
<h4 id="数据完整性"><a href="#数据完整性" class="headerlink" title="数据完整性"></a>数据完整性</h4><h5 id="保证实体完整性"><a href="#保证实体完整性" class="headerlink" title="保证实体完整性"></a>保证实体完整性</h5><p>1、    主键约束<br>2、    唯一约束<br>3、    自动增长列</p>
<h5 id="保证域完整性"><a href="#保证域完整性" class="headerlink" title="保证域完整性"></a>保证域完整性</h5><p>1、    数据类型约束<br>2、    非空约束<br>3、    默认值约束</p>
<h5 id="保证引用完整性"><a href="#保证引用完整性" class="headerlink" title="保证引用完整性"></a>保证引用完整性</h5><p>1、外键约束：从表中的公共字段是主表的外键</p>
<h5 id="保证自定义完整性"><a href="#保证自定义完整性" class="headerlink" title="保证自定义完整性"></a>保证自定义完整性</h5><p>1、    存储过程<br>2、    触发器</p>
<h5 id="主表和从表"><a href="#主表和从表" class="headerlink" title="主表和从表"></a>主表和从表</h5><p>两个表建立关系（两个表只要有公共字段就有关系），一个表称为主表，一个表称为从表。<br>外键约束可以实现：<br>1、    主表中没有的从表中不允许插入<br>2、    从表中有的主表中不允许删除<br>3、    不能更改主表中的值而导致从表中的记录孤立存在。<br>4、    先删除从表，再删除主表</p>
<h4 id="外键（foreign-key）"><a href="#外键（foreign-key）" class="headerlink" title="外键（foreign key）"></a>外键（foreign key）</h4><p>1、    外键：从表中的公共字段，公共字段的名字可以不一样，但是数据类型必须一样。<br>2、    外键约束用来保证引用完整性</p>
<h5 id="添加外键"><a href="#添加外键" class="headerlink" title="添加外键"></a>添加外键</h5><p><strong>foreign key (id) references stuinfo(stuno);</strong><br>方法一：创建表的时候添加外键</p>
<p>references：参照</p>
<p>测试：</p>
<ul>
<li>主表中没有的，从表中不允许插入</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">先插主表，在插从表</div><div class="line"></div><div class="line">mysql&gt; insert into stuinfo values (1,&apos;tom&apos;);</div><div class="line">Query OK, 1 row affected (0.00 sec)</div><div class="line">mysql&gt; insert into stumarks values (1,77);</div><div class="line">Query OK, 1 row affected (0.00 sec)</div></pre></td></tr></table></figure>
<ul>
<li><p>从表中有的，主表中不能删除</p>
</li>
<li><p>不能更改主表的数据后使得从表的数据孤立</p>
</li>
</ul>
<p>方法二：修改表的时候添加外键<br>语法：<code>alter table stumarks add foreign key (id) references stuinfo(stuno);</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table stuinfo(</div><div class="line">    -&gt; stuno char(4) primary key,</div><div class="line">    -&gt; stuname varchar(20) not null</div><div class="line">    -&gt; );</div><div class="line">Query OK, 0 rows affected (0.00 sec)</div><div class="line"></div><div class="line">mysql&gt; create table stumarks(</div><div class="line">    -&gt; id char(4) primary key,</div><div class="line">    -&gt; score int</div><div class="line">    -&gt; );</div><div class="line">Query OK, 0 rows affected (0.00 sec)</div><div class="line"></div><div class="line">mysql&gt; alter table stumarks add foreign key (id) references stuinfo(stuno);</div><div class="line">Query OK, 0 rows affected (0.01 sec)</div><div class="line">Records: 0  Duplicates: 0  Warnings: 0</div></pre></td></tr></table></figure>
<blockquote>
<p>要创建外键必须是innodb引擎，myisam不支持外键约束</p>
</blockquote>
<p><strong>创建外键的时候，给外键取名</strong><br><code>alter table stumarks add constraint `FK1` foreign key(id) references stuinfo(stuno);</code></p>
<blockquote>
<p>constraint：约束</p>
</blockquote>
<h5 id="删除外键"><a href="#删除外键" class="headerlink" title="删除外键"></a>删除外键</h5><p>通过外键的名字删除外键<br>语法：<code>alter table 表名 drop foreign key `外键名` </code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mysql&gt;alter talbe stumarks drop foreign key `FK1`</div></pre></td></tr></table></figure>
<blockquote>
<p>如果要在某个字段上添加外键约束，改字段必须要有索引才可以。如果该字段已经存在索引，则直接使用，否则MySQL自动生成索引。</p>
</blockquote>
<h5 id="外键操作"><a href="#外键操作" class="headerlink" title="外键操作"></a>外键操作</h5><p>1、    严格操作（前面讲的是严格操作）<br>2、    置空操作（set null）：如果主表记录删除或更新，从表置空<br>3、    级联操作（cascade）：如果主表记录删除或更新，从表级联</p>
<blockquote>
<p>一般来说：主表删除的时候，从表置空操作，主表更新的时候，从表级联操作。<br>语法：foreign key(外键) references 主表(关键字段) [主表删除时候的动作] [主表更新时候的动作]</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table stumarks(</div><div class="line">    -&gt; id int auto_increment primary key,</div><div class="line">    -&gt; stuno char(4),</div><div class="line">    -&gt; stuscore tinyint(3),</div><div class="line">    -&gt; foreign key(stuno) references stuinfo(stuno) on delete set null on update cascade</div><div class="line">    -&gt; );</div><div class="line">Query OK, 0 rows affected (0.03 sec)</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line">--错误案例--</div><div class="line">create table stuinfo(</div><div class="line">stuno char(4) primary key,</div><div class="line">stuname varchar(20) not null</div><div class="line">);</div><div class="line"></div><div class="line">create table stumarks(</div><div class="line">id char(4) primary key,</div><div class="line">score int</div><div class="line">);</div><div class="line"></div><div class="line">alter table stumarks add constraint FK1 foreign key (id) references stuinfo(stuno) on delete set null on update cascade;</div><div class="line"></div><div class="line">--正确案例--</div><div class="line">create table stuinfo(</div><div class="line">stuno char(4) primary key,</div><div class="line">stuname varchar(20) not null</div><div class="line">);</div><div class="line"></div><div class="line">create table stumarks(</div><div class="line">id1 char(4) primary key,</div><div class="line">id char(4),</div><div class="line">score int</div><div class="line">);</div><div class="line"></div><div class="line">alter table stumarks add constraint FK1 foreign key (id) references stuinfo(stuno) on delete set null on update cascade;</div></pre></td></tr></table></figure>
<blockquote>
<p>使用alter修改表 去创建外键时，是否不能够同时指定制空约束和级联约束？<br>主键不能set null ,所以外键表中新增主键，将外键作为非主键，此问题与alter的设置无关<br><code>foreign key (stuno) references stuinfo(stuno) on delete set null on update cascade;</code></p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table stuinfo(</div><div class="line">    -&gt; stuno char(4) primary key,</div><div class="line">    -&gt; stuname varchar(20) not null</div><div class="line">    -&gt; );</div><div class="line">Query OK, 0 rows affected (0.02 sec)</div><div class="line">//主键与非主键之间的关系，主键是不能set null的，所有使用非主键作为外键</div><div class="line">mysql&gt; create table stumarks(</div><div class="line">    -&gt; id int auto_increment primary key,</div><div class="line">    -&gt; stuno char(4),</div><div class="line">	    -&gt; score int,</div><div class="line">	    -&gt; foreign key (stuno) references stuinfo(stuno) on delete set null on update cascade</div><div class="line">    -&gt; );</div><div class="line">Query OK, 0 rows affected (0.00 sec)</div><div class="line"></div><div class="line">mysql&gt; insert into stuinfo values (&apos;1001&apos;,&apos;tom&apos;),(&apos;1002&apos;,&apos;berry&apos;);</div><div class="line">Query OK, 2 rows affected (0.00 sec)</div><div class="line">Records: 2  Duplicates: 0  Warnings: 0</div><div class="line"></div><div class="line">mysql&gt; insert into stumarks values (null,&apos;1001&apos;,88),(null,&apos;1002&apos;,99);</div><div class="line">Query OK, 2 rows affected (0.00 sec)</div><div class="line">Records: 2  Duplicates: 0  Warnings: 0</div><div class="line">mysql&gt; select * from stumarks;</div><div class="line">+----+-------+-------+</div><div class="line">| id | stuno | score |</div><div class="line">+----+-------+-------+</div><div class="line">|  1 | 1001  |    88 |</div><div class="line">|  2 | 1002  |    99 |</div><div class="line">+----+-------+-------+</div><div class="line">2 rows in set (0.00 sec)</div><div class="line"></div><div class="line">mysql&gt; select * from stuinfo;</div><div class="line">+-------+---------+</div><div class="line">| stuno | stuname |</div><div class="line">+-------+---------+</div><div class="line">| 1001  | tom     |</div><div class="line">| 1002  | berry   |</div><div class="line">+-------+---------+</div><div class="line">2 rows in set (0.00 sec)</div><div class="line"></div><div class="line">mysql&gt; delete from stuinfo where stuno=&apos;1001&apos;;</div><div class="line">Query OK, 1 row affected (0.00 sec)</div><div class="line"></div><div class="line">mysql&gt; select * from stumarks;</div><div class="line">+----+-------+-------+</div><div class="line">| id | stuno | score |</div><div class="line">+----+-------+-------+</div><div class="line">|  1 | NULL  |    88 |</div><div class="line">|  2 | 1002  |    99 |</div><div class="line">+----+-------+-------+</div><div class="line">2 rows in set (0.00 sec)</div><div class="line"></div><div class="line">mysql&gt; update stuinfo set stuno=&apos;1003&apos; where stuno=&apos;1002&apos;;</div><div class="line">Query OK, 1 row affected (0.00 sec)</div><div class="line">Rows matched: 1  Changed: 1  Warnings: 0</div><div class="line"></div><div class="line">mysql&gt; select * from stumarks;</div><div class="line">+----+-------+-------+</div><div class="line">| id | stuno | score |</div><div class="line">+----+-------+-------+</div><div class="line">|  1 | NULL  |    88 |</div><div class="line">|  2 | 1003  |    99 |</div><div class="line">+----+-------+-------+</div><div class="line">2 rows in set (0.00 sec)</div></pre></td></tr></table></figure>
<h3 id="再来一戳，外键级联"><a href="#再来一戳，外键级联" class="headerlink" title="再来一戳，外键级联"></a>再来一戳，外键级联</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">ALTER TABLE stumarks DROP FOREIGN KEY `FK1`;</div><div class="line"></div><div class="line">DROP TABLE</div><div class="line">IF EXISTS stuinfo;</div><div class="line"></div><div class="line">DROP TABLE</div><div class="line">IF EXISTS stumarks;</div><div class="line"></div><div class="line">CREATE TABLE stuinfo(</div><div class="line">    stuno CHAR(4) PRIMARY KEY ,</div><div class="line">    stuname VARCHAR(10)</div><div class="line">);</div><div class="line"></div><div class="line">CREATE TABLE stumarks(</div><div class="line">    id INT auto_increment PRIMARY KEY ,</div><div class="line">    stuno CHAR(4) UNIQUE KEY,</div><div class="line">    stuscore TINYINT(3) ,</div><div class="line">    CONSTRAINT `FK1` FOREIGN KEY(stuno) REFERENCES stuinfo(stuno) ON DELETE</div><div class="line">SET NULL ON UPDATE CASCADE</div><div class="line">);</div></pre></td></tr></table></figure>
<blockquote>
<p>经验总结：设置级联外键由于 on delete set null 的原因不能设置外键为主键，解决外键数据完整的方法是给外键增加为一件 unique key;</p>
</blockquote>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/MySQL巩固与加强/" rel="tag">#MySQL巩固与加强</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/20160829-1.html" rel="next" title="Day7-MySQL 知识、数据库、数据表">
                <i class="fa fa-chevron-left"></i> Day7-MySQL 知识、数据库、数据表
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/20160901-1.html" rel="prev" title="Day9-MySQL 数据库设计及步骤">
                Day9-MySQL 数据库设计及步骤 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <!-- Go to www.addthis.com/dashboard to customize your tools -->
<script type = "text/javascript" src = "//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-57838eee5e2620d0" async = "async" ></script>
      
    </div>
  </div>


          </div>
          

  <p>热评文章</p>
  <div class="ds-top-threads" data-range="weekly" data-num-items="4"></div>


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="20160830-1.html"
           data-title="Day8-MySQL 数据类型、列属性、数据完整性" data-url="http://blog.uiste.com/20160830-1.html">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.png"
               alt="uiste" />
          <p class="site-author-name" itemprop="name">uiste</p>
          <p class="site-description motion-element" itemprop="description">Stay Hungry，Stay Foolish</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">70</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              
                <span class="site-state-item-count">13</span>
                <span class="site-state-item-name">分类</span>
              
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">55</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/uiste" target="_blank" rel="external nofollow" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/uiste" target="_blank" rel="external nofollow" title="微博">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  微博
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.zhihu.com/people/uiste" target="_blank" rel="external nofollow" title="知乎">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              友情链接
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.uiste.com" title="uiste 个人博客" target="_blank">uiste 个人博客</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://uiste.github.io" title="uiste 技术博客" target="_blank">uiste 技术博客</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#数据类型"><span class="nav-number">1.</span> <span class="nav-text">数据类型</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#数据类型——值类型"><span class="nav-number">1.1.</span> <span class="nav-text">数据类型——值类型</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#整型"><span class="nav-number">1.1.1.</span> <span class="nav-text">整型</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#浮点型（保存近似值小数）"><span class="nav-number">1.1.2.</span> <span class="nav-text">浮点型（保存近似值小数）</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#数据类型——字符型"><span class="nav-number">1.2.</span> <span class="nav-text">数据类型——字符型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#数据类型——枚举（enum）"><span class="nav-number">1.3.</span> <span class="nav-text">数据类型——枚举（enum）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#数据类型——集合（set"><span class="nav-number">1.4.</span> <span class="nav-text">数据类型——集合（set)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#数据类型——日期类型"><span class="nav-number">1.5.</span> <span class="nav-text">数据类型——日期类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#数据类型——boolean"><span class="nav-number">1.6.</span> <span class="nav-text">数据类型——boolean</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#创建数据表的数据类型"><span class="nav-number">1.7.</span> <span class="nav-text">创建数据表的数据类型</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#列属性-是否为空-null-not-null"><span class="nav-number">2.</span> <span class="nav-text">列属性-是否为空(null | not null)</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#列属性——默认值（default）"><span class="nav-number">2.1.</span> <span class="nav-text">列属性——默认值（default）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#列属性——自动增长（auto-increment）"><span class="nav-number">2.2.</span> <span class="nav-text">列属性——自动增长（auto_increment）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#列属性——主键（primary-key）"><span class="nav-number">2.3.</span> <span class="nav-text">列属性——主键（primary key）</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#添加主键：方法一【创建表的时候添加主键】"><span class="nav-number">2.3.1.</span> <span class="nav-text">添加主键：方法一【创建表的时候添加主键】</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#添加主键：方法二【创建表的时候添加主键】"><span class="nav-number">2.3.2.</span> <span class="nav-text">添加主键：方法二【创建表的时候添加主键】</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#添加主键：方法三【更改表的时候添加主键】"><span class="nav-number">2.3.3.</span> <span class="nav-text">添加主键：方法三【更改表的时候添加主键】</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#查看主键"><span class="nav-number">2.3.4.</span> <span class="nav-text">查看主键</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#创建组合键"><span class="nav-number">2.3.5.</span> <span class="nav-text">创建组合键</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#删除主键"><span class="nav-number">2.3.6.</span> <span class="nav-text">删除主键</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#选择主键的原则"><span class="nav-number">2.3.7.</span> <span class="nav-text">选择主键的原则</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#主键思考题"><span class="nav-number">2.3.8.</span> <span class="nav-text">主键思考题</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#列属性——唯一键"><span class="nav-number">2.4.</span> <span class="nav-text">列属性——唯一键</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#添加唯一键【创建表的时候添加唯一键】"><span class="nav-number">2.4.1.</span> <span class="nav-text">添加唯一键【创建表的时候添加唯一键】</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#添加唯一键【创建表的时候添加唯一键】-1"><span class="nav-number">2.4.2.</span> <span class="nav-text">添加唯一键【创建表的时候添加唯一键】</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#设置名字的唯一键"><span class="nav-number">2.4.3.</span> <span class="nav-text">设置名字的唯一键</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#添加唯一键【修改表的时候添加唯一键】"><span class="nav-number">2.4.4.</span> <span class="nav-text">添加唯一键【修改表的时候添加唯一键】</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#查看唯一键"><span class="nav-number">2.4.5.</span> <span class="nav-text">查看唯一键</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#添加组合唯一键"><span class="nav-number">2.4.6.</span> <span class="nav-text">添加组合唯一键</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#删除唯一键"><span class="nav-number">2.4.7.</span> <span class="nav-text">删除唯一键</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#列属性——备注（comment）"><span class="nav-number">2.4.8.</span> <span class="nav-text">列属性——备注（comment）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#SQL注释"><span class="nav-number">2.4.9.</span> <span class="nav-text">SQL注释</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#数据完整性"><span class="nav-number">2.5.</span> <span class="nav-text">数据完整性</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#保证实体完整性"><span class="nav-number">2.5.1.</span> <span class="nav-text">保证实体完整性</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#保证域完整性"><span class="nav-number">2.5.2.</span> <span class="nav-text">保证域完整性</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#保证引用完整性"><span class="nav-number">2.5.3.</span> <span class="nav-text">保证引用完整性</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#保证自定义完整性"><span class="nav-number">2.5.4.</span> <span class="nav-text">保证自定义完整性</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#主表和从表"><span class="nav-number">2.5.5.</span> <span class="nav-text">主表和从表</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#外键（foreign-key）"><span class="nav-number">2.6.</span> <span class="nav-text">外键（foreign key）</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#添加外键"><span class="nav-number">2.6.1.</span> <span class="nav-text">添加外键</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#删除外键"><span class="nav-number">2.6.2.</span> <span class="nav-text">删除外键</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#外键操作"><span class="nav-number">2.6.3.</span> <span class="nav-text">外键操作</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#再来一戳，外键级联"><span class="nav-number">3.</span> <span class="nav-text">再来一戳，外键级联</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">uiste</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="http://hexo.io" rel="external nofollow">Hexo</a> 趋势动力
</div>
<!--站长统计-->
<script src="https://s95.cnzz.com/z_stat.php?id=1259894121&web_id=1259894121" language="JavaScript"></script>
<!--<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next" rel="external nofollow">
    NexT.Mist
  </a>
</div> -->
&nbsp;&nbsp;&nbsp;本站总点击 <span id="busuanzi_value_site_pv"></span> 次
&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;您是第 <span id="busuanzi_value_site_uv"></span> 位访客

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>

<script>
(function(){
    var bp = document.createElement('script');
    bp.src = '//push.zhanzhang.baidu.com/push.js';
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"uiste"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/vendors/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  
  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
       search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body").append('<div class="popoverlay">').css('overflow', 'hidden');
      $('.popup').toggle();

    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
    'use strict';
    $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
            // get the contents from search data
            isfetched = true;
            $('.popup').detach().appendTo('.header-inner');
            var datas = $( "entry", xmlResponse ).map(function() {
                return {
                    title: $( "title", this ).text(),
                    content: $("content",this).text(),
                    url: $( "url" , this).text()
                };
            }).get();
            var $input = document.getElementById(search_id);
            var $resultContent = document.getElementById(content_id);
            $input.addEventListener('input', function(){
                var matchcounts = 0;
                var str='<ul class=\"search-result-list\">';
                var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                $resultContent.innerHTML = "";
                if (this.value.trim().length > 1) {
                // perform local searching
                datas.forEach(function(data) {
                    var isMatch = true;
                    var content_index = [];
                    var data_title = data.title.trim().toLowerCase();
                    var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                    var data_url = data.url;
                    var index_title = -1;
                    var index_content = -1;
                    var first_occur = -1;
                    // only match artiles with not empty titles and contents
                    if(data_title != '' && data_content != '') {
                        keywords.forEach(function(keyword, i) {
                            index_title = data_title.indexOf(keyword);
                            index_content = data_content.indexOf(keyword);
                            if( index_title < 0 && index_content < 0 ){
                                isMatch = false;
                            } else {
                                if (index_content < 0) {
                                    index_content = 0;
                                }
                                if (i == 0) {
                                    first_occur = index_content;
                                }
                            }
                        });
                    }
                    // show search results
                    if (isMatch) {
                        matchcounts += 1;
                        str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                        var content = data.content.trim().replace(/<[^>]+>/g,"");
                        if (first_occur >= 0) {
                            // cut out 100 characters
                            var start = first_occur - 20;
                            var end = first_occur + 80;
                            if(start < 0){
                                start = 0;
                            }
                            if(start == 0){
                                end = 50;
                            }
                            if(end > content.length){
                                end = content.length;
                            }
                            var match_content = content.substring(start, end);
                            // highlight all keywords
                            keywords.forEach(function(keyword){
                                var regS = new RegExp(keyword, "gi");
                                match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                            });

                            str += "<p class=\"search-result\">" + match_content +"...</p>"
                        }
                        str += "</li>";
                    }
                })};
                str += "</ul>";
                if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
                if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
                $resultContent.innerHTML = str;
            });
            proceedsearch();
        }
    });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };

    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".popoverlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>


  

  

  

</body>
</html>
